﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ConvertSourceToHtml.Properties;
using ConvertSourceToHtmlCustomControls;

namespace ConvertSourceToHtml
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        private Brush _validBrush = Brushes.White;
        private Brush _invalidBrush = Brushes.Yellow;

        public SettingsWindow()
        {
            InitializeComponent();
        }

        private bool ValidateFormData()
        {
            bool dataValid = true;

            if ((bool)numberLinesCheckBox.IsChecked)
            {
                int firstLineNumber;
                bool firstLineNumberIsNumber = int.TryParse(numberingStartTextBox.Text, out firstLineNumber);
                if (!firstLineNumberIsNumber || firstLineNumber < 1)
                {
                    dataValid = false;
                    numberingStartTextBox.Background = _invalidBrush;
                }
                else
                {
                    numberingStartTextBox.Background = _validBrush;
                }
            }

            int fontSize;
            bool fontSizeIsNumber = int.TryParse(fontSizeTextBox.Text, out fontSize);
            if (!fontSizeIsNumber || fontSize < 1)
            {
                dataValid = false;
                fontSizeTextBox.Background = _invalidBrush;
            }
            else
            {
                fontSizeTextBox.Background = _validBrush;
            }

            dataValid &= ValidateColorComponent(backgroundColorRTextBox)
                       & ValidateColorComponent(backgroundColorGTextBox)
                       & ValidateColorComponent(backgroundColorBTextBox);
            if ((bool)numberLinesCheckBox.IsChecked)
            {
                dataValid &= ValidateColorComponent(lineNumberColorRTextBox)
                           & ValidateColorComponent(lineNumberColorGTextBox)
                           & ValidateColorComponent(lineNumberColorBTextBox);
            }

            return dataValid;
        }

        private bool ValidateColorComponent(TextBox componentTextBox)
        {
            byte componentValue;
            bool isValidByte = byte.TryParse(componentTextBox.Text, out componentValue);

            if (isValidByte && (componentValue >= 0) && (componentValue <= 255))
            {
                componentTextBox.Background = _validBrush;
                return true;
            }
            else
            {
                componentTextBox.Background = _invalidBrush;
                return false;
            }
        }

        private void SaveSettings()
        {
            UpdateBindingSource(numberLinesCheckBox, CheckBox.IsCheckedProperty);
            if ((bool)numberLinesCheckBox.IsChecked)
                UpdateBindingSource(numberingStartTextBox, TextBox.TextProperty);
            UpdateBindingSource(fontFamilyTextBox, TextBox.TextProperty);
            UpdateBindingSource(fontSizeTextBox, TextBox.TextProperty);
            UpdateBindingSource(backgroundColorRTextBox, TextBox.TextProperty);
            UpdateBindingSource(backgroundColorGTextBox, TextBox.TextProperty);
            UpdateBindingSource(backgroundColorBTextBox, TextBox.TextProperty);
            if ((bool)numberLinesCheckBox.IsChecked)
            {
                UpdateBindingSource(lineNumberColorRTextBox, TextBox.TextProperty);
                UpdateBindingSource(lineNumberColorGTextBox, TextBox.TextProperty);
                UpdateBindingSource(lineNumberColorBTextBox, TextBox.TextProperty);
            }
            UpdateBindingSource(alternateBackgroundCheckBox, CheckBox.IsCheckedProperty);
            if ((bool)alternateBackgroundCheckBox.IsChecked)
                UpdateBindingSource(variationPercentageNumericUpDown, NumericUpDown.ValueProperty);
            
            Settings.Default.Save();
        }

        private void UpdateBindingSource(FrameworkElement element, DependencyProperty property)
        {
            BindingExpression bindingExpression = element.GetBindingExpression(property);
            bindingExpression.UpdateSource();
        }

        private void saveButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ValidateFormData() == true)
            {
                SaveSettings();
                Close();
            }
        }

        private void cancelButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                saveButton_MouseLeftButtonUp(null, null);
            else if (e.Key == Key.Escape)
                cancelButton_MouseLeftButtonUp(null, null);
        }

        private void windowBackground_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.Source == windowBackground)
                this.DragMove();
        }
    }
}
